package minimips.computer;

import java.util.ArrayList;
import java.util.List;

public class Register {

	private long registerValue;
	private List<LoadStoreListener<Register>> listeners;
	
	public Register() {
		this.listeners = new ArrayList<LoadStoreListener<Register>>();
	}
	
	public Register(long registerValue) {
		this();
		this.registerValue = registerValue;
	}
	
	public long getRegisterValue() {
		fireRegisterLoadListener();
		return this.registerValue;
	}
	
	public void setRegisterValue(long newValue) {
		this.registerValue = newValue;
		fireRegisterStoreListener();
	}
	
	public void setRegisterValue(Register register) {
		this.registerValue = register.getRegisterValue();
		fireRegisterStoreListener();
	}
	
	public void addRegisterChangeListener(LoadStoreListener<Register> listener) {
		this.listeners.add(listener);
	}
	
	private void fireRegisterStoreListener() {
		for (LoadStoreListener<Register> listener : listeners) {
			listener.onStore(new LoadStoreEventObject<Register>(this));
		}
	}
	
	private void fireRegisterLoadListener() {
		for (LoadStoreListener<Register> listener : listeners) {
			listener.onLoad(new LoadStoreEventObject<Register>(this));
		}
	}
}
